package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.Role;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.w3c.dom.css.CSSPageRule;

import java.util.List;
import java.util.UUID;

/**
 * @author 刘昱江
 * 时间 2021/2/2
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public String login(User user) {
        String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);
        User userDB = userMapper.selectOne(new QueryWrapper<>(user));
        if(userDB !=null ){
            String uuid = UUID.randomUUID().toString().replace("-", "");
            return uuid;
        }
        return null;
    }

    //实现分页查询
    @Override
    public PageResult findUserList(PageResult pageResult) {
        //1.查询总记录数
        long total = userMapper.selectCount(null);
        //2.分页查询
        int start = (pageResult.getPageNum() - 1) * pageResult.getPageSize();
        List<User> userList = userMapper.findUserListByPage(start,pageResult.getPageSize(),pageResult.getQuery());
        pageResult.setTotal(total).setRows(userList);
        return pageResult;
        /*IPage iPage = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(pageResult.getQuery() !=null && !"".equals(pageResult.getQuery()), "username", pageResult.getQuery());
        iPage = userMapper.selectPage(iPage, queryWrapper);
        pageResult.setTotal(iPage.getTotal());
        List<User> userList = iPage.getRecords();
        pageResult.setRows(iPage.getRecords());*/
    }

    @Override
    public void updateStatus(User user) {

        userMapper.updateById(user);
    }

    @Override
    public void addUser(User user) {
        //将密码进行加密处理
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(password);
        //设置为启用状态
        user.setStatus(true);
        userMapper.insert(user);
    }

    @Override
    public User findUserById(Integer id) {

        return userMapper.selectById(id);
    }

    @Override
    public void updateUser(User user) {

        userMapper.updateById(user);
    }

    @Override
    public void deleteUserById(Integer id) {

        userMapper.deleteById(id);
        userMapper.deleteUserRole(id);
    }

    @Override
    public void saveUserRole(Integer userId, Integer roleId) {
        userMapper.deleteUserRole(userId);
        userMapper.saveUserRole(userId,roleId);
    }


}
